﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medecine.Interface.Main;
using Medecine.DataAccess.Users;
using GalaSoft.MvvmLight.Command;
using Medecine.Interface.Properties;
using GalaSoft.MvvmLight.Messaging;
using Medecine.Dbo;
using System.Windows;
using System.IO;
using System.Reactive.Linq;

namespace Medecine.Interface.Users
{
	public class AddUserViewModel : ViewModelWithErrors
	{
		public AddUserViewModel()
		{
			_user = new User();
			_dropCommand = new RelayCommand<DragEventArgs>(Drop, CanDrop);
			_addCommand = new RelayCommand(Add);
			_cancelCommand = new RelayCommand(Cancel);
		}

		#region User

		private readonly User _user;
		public User User
		{
			get { return _user; }
		}

		public string UserName
		{
			get { return _user.Login; }
			set
			{
				if (_user.Login == value)
					return;
				_user.Login = value;
				RaisePropertyChanged("UserName");
			}
		}

		public string FirstName
		{
			get { return _user.Firstname; }
			set
			{
				if (_user.Firstname == value)
					return;
				_user.Firstname = value;
				RaisePropertyChanged("FirstName");
			}
		}

		public string LastName
		{
			get { return _user.Name; }
			set
			{
				if (_user.Name == value)
					return;
				_user.Name = value;
				RaisePropertyChanged("LastName");
			}
		}

		public string Role
		{
			get { return _user.Role; }
			set
			{
				if (_user.Role == value)
					return;
				_user.Role = value;
				RaisePropertyChanged("Role");
			}
		}

		#endregion // User

		#region Picture

		public byte[] Picture
		{
			get { return _user.Picture; }
			set
			{
				if (_user.Picture == value)
					return;
				_user.Picture = value;
				RaisePropertyChanged("Picture");
			}
		}

		#region Drop

		private readonly RelayCommand<DragEventArgs> _dropCommand = null;
		public RelayCommand<DragEventArgs> DropCommand { get { return _dropCommand; } }

		private bool CanDrop(DragEventArgs dragData)
		{
			return dragData.Data != null;
		}

		private void Drop(DragEventArgs dragData)
		{
			string[] fileArray = (string[])dragData.Data.GetData(DataFormats.FileDrop);

			if (fileArray.Length != 0)
			{
				string file = fileArray[0];

				if ((Path.GetExtension(file) == ".jpg")
					|| (Path.GetExtension(file) == ".jpeg")
					|| (Path.GetExtension(file) == ".png"))
				{
					using (FileStream stream = new FileStream(file, FileMode.Open))
					{
						BinaryReader reader = new BinaryReader(stream);
						Picture = reader.ReadBytes((int)stream.Length);
					}
				}
			}
		}

		#endregion // Drop

		#endregion // Picture

		#region Add

		private readonly RelayCommand _addCommand;
		public RelayCommand AddCommand { get { return _addCommand; } }

		private void Add()
		{
			using (ServiceUserClient service = new ServiceUserClient())
			{
				Observable.FromAsyncPattern<User, bool>(service.BeginAddUser, service.EndAddUser)
					.Invoke(User)
					.ObserveOnDispatcher()
					.Subscribe(AddCompleted, (e) => Messenger.Default.Send(new ErrorMessage(e.Message)));

				bool result = service.AddUser(User);
				
			}
		}

		private void AddCompleted(bool result)
		{
			if (result == false)
				Messenger.Default.Send(new ErrorMessage(Resources.AddUserError));
			else
			{
				Cancelled = false;
				Messenger.Default.Send(new GoToViewMessage(ViewName.Home));
			}
		}

		#endregion // Add

		#region Cancel

		private bool _cancelled = true;
		public bool Cancelled
		{
			get { return _cancelled; }
			private set { _cancelled = value; }
		}

		private readonly RelayCommand _cancelCommand;
		public RelayCommand CancelCommand { get { return _cancelCommand; } }

		private void Cancel()
		{
			Messenger.Default.Send(new GoToViewMessage(ViewName.Home));
		}

		#endregion // Cancel
	}
}
